package com.dragon.controller;

import com.dragon.pojo.Record.RecordSelectInfo;
import com.dragon.pojo.Result;
import com.dragon.service.RecordService;
import com.dragon.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("record")
public class RecordController {
    @Autowired
    RecordService recordService;

    /**
     * 查询提交记录列表
     * @param info RecordSelectInfo
     * @return 返回响应
     */
    @GetMapping("list")
    public Result GetRecord(RecordSelectInfo info) {

        return Result.success(recordService.getRecord(info));
    }

    /**
     * 查询提交记录信息
     * @param request 获取 token
     * @param recordId 记录id
     * @return 返回响应
     */
    @GetMapping("info/{recordId}")
    public Result GetRecordInfo(HttpServletRequest request, @PathVariable Integer recordId) {
        String jwt = request.getHeader("Accept-token");
        Integer userId = null;
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                userId = (Integer) claims.get("id");
            }
        }
        return Result.success(recordService.getRecordInfo(recordId, userId));
    }
}
